<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程班级" prop="claId">
        <el-input
          v-model="queryParams.claId"
          placeholder="请输入课程班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期" prop="beginDate">
        <el-date-picker clearable
          v-model="queryParams.beginDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="任课教师" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入任课教师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上课教学楼" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入上课教学楼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上课教室号" prop="roomId">
        <el-input
          v-model="queryParams.roomId"
          placeholder="请输入上课教室号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程" prop="course">
        <el-input
          v-model="queryParams.course"
          placeholder="请输入课程"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:rule:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:rule:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:rule:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:rule:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ruleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="规则id" align="center" prop="ruleId" />
      <el-table-column label="课程班级" align="center" prop="claId" />
      <el-table-column label="开始日期" align="center" prop="beginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上课时间" align="center" prop="startTime" />
      <el-table-column label="下课时间" align="center" prop="endTime" />
      <el-table-column label="任课教师" align="center" prop="teacherId" />
      <el-table-column label="上课教学楼" align="center" prop="roomName" />
      <el-table-column label="上课教室号" align="center" prop="roomId" />
      <el-table-column label="课程" align="center" prop="course" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:rule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:rule:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改上课时间配置规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body class="add-form auto-width">

      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
        <el-col :span="12">
        <el-form-item label="课程" prop="course">
          <el-select @change="getClaOptions"
            v-model="form.course"
            filterable
            allow-create
            placeholder="选择课程"
            clearable
            default-first-option
          >
            <el-option
              v-for="course in courseOptions"
              :key="course.coursename"
              :label="course.coursename"
              :value="course.coursename"
            />
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班级" prop="claId">
          <el-select
                     v-model="form.claId"
                     filterable
                     allow-create
                     placeholder="选择班级"
                     clearable
                     default-first-option
                     @change="getTeacherId"
          >
            <el-option
              v-for="cla in claOptions"
              :key="cla.claId"
              :label="cla.claName"
              :value="cla.claId"
            />
          </el-select>
          </el-form-item>

        </el-col>
        </el-row>
<el-row>
        <el-col :span="12">
        <el-form-item label="开始日期" prop="beginDate">
          <el-date-picker clearable
            v-model="form.beginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
          </el-col>
</el-row>
        <el-form-item label="每周上课时间" label-width="100px">
          <el-checkbox v-model="checkAllWeekDay" :indeterminate="isIndeterminate" @change="handleCheckAllChange">全选</el-checkbox>
          <el-checkbox-group v-model="form.weekDay" @change="handleCheckedChange">

            <el-checkbox
              v-for="dict in weekOptions"
              :key="dict.dictValue"
              :label="dict.dictValue">
              {{dict.dictLabel}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-row>
        <el-col :span="12">
          <el-form-item label="上课时间:" prop="startTime"  :key="form.startTime">
            <el-time-select
              v-model="form.startTime"
              size="small"
              :picker-options="{
                start: '08:00',
                step: '00:30',
                end: '19:00'
              }"
              placeholder="选择上课时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="下课时间:" prop="endTime" :key="form.endTime">
            <el-time-select
              v-model="form.endTime"
              size="small"
              :picker-options="{
                start: '08:00',
                step: '00:30',
                end: '20:00'
              }"
              placeholder="选择下课时间"
            />
          </el-form-item>
        </el-col>
        </el-row>

        <el-form-item label="上课教学楼" prop="roomName" label-width="100px">
          <el-input v-model="form.roomName"   placeholder="请输入上课教学楼"  />
        </el-form-item>
        <el-form-item label="上课教室号" prop="roomId" label-width="100px">
          <el-input v-model="form.roomId" placeholder="请输入上课教室号" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRule, getRule, delRule, addRule, updateRule } from "@/api/system/rule";
import {getDicts} from "@/api/system/dict/data";
import {listCourse as CourseSelect} from '@/api/system/course'
import {getCla, simpleListCla} from '@/api/system/cla'
export default {
  name: "Rule",
  data() {
    return {
      // 班级
      claOptions:[],
      // 科目
      courseOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //全选条件
      isIndeterminate: true,
      checkAllWeekDay: false,
      // 上课时间配置规则表格数据
      ruleList: [],
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        claId: null,
        beginDate: null,
        startTime: '',
        teacherId: null,
        roomName: null,
        roomId: null,
        course: null,
      },
      //依据课表执行班级查询
      queryy:{
        vall:null,
      },

      weekOptions:[],
      // 表单参数
      form: {weekDay:[]},
      // 表单校验
      rules: {
        claId: [
          { required: true, message: "课程班级不能为空", trigger: "blur" }
        ],
        beginDate: [
          { required: true, message: "开始日期不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ],
        weekDay: [
          { required: true, message: "每周上课时间不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "上课时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "下课时间不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "任课教师不能为空", trigger: "blur" }
        ],
        roomName: [
          { required: true, message: "上课教学楼不能为空", trigger: "blur" }
        ],
        roomId: [
          { required: true, message: "上课教室号不能为空", trigger: "blur" }
        ],
        course: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getdictweek();
    this.getCourseOptions()

  },
  methods: {
    /** 查询科目列表 */
    getCourseOptions() {

      CourseSelect().then(response => {
        this.courseOptions = response.rows;


      })
    },


    /** 班级对应教师*/
    getTeacherId(val) {
      this.form.teacherId=null

      if (val!==null &val!== '') {
        getCla(val).then(response => {

          this.form.teacherId = response.data.staffId;
        })
      }
    },

    /** 选择科目 查询班级 */
    getClaOptions(val) {
      this.queryy.vall=val
      this.claOptions=null
      this.form.claId=null
      if (val!==null &val!== ''){
      simpleListCla(this.queryy).then(response => {
        this.claOptions = response.data;

      })}
    },
    /** 查询上课时间配置规则列表 */
    getdictweek() {
      getDicts("sys_week_day").then(response => {

        this.weekOptions = response.data;
      });
    },
    /** 查询上课时间配置规则列表 */
    getList() {
      this.loading = true;
      listRule(this.queryParams).then(response => {
        this.ruleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ruleId: null,
        claId: null,
        beginDate: null,
        endDate: null,
        weekDay: [],
        startTime: '',
        endTime: '',
        teacherId: null,
        roomName: null,
        roomId: null,
        course: null,
        createUser: null,
        createTime: null,
        lastUpdateUser: null,
        lastUpdateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ruleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加上课时间配置规则";
    },
    /** 全选按钮操作 */
    handleCheckAllChange(val) {
      this.form.weekDay = val ? this.weekOptions.map(item => {
        return item.dictValue
      }) : [];
      this.isIndeterminate = true;
    },
    handleCheckedChange(value) {
      dict.type.sys_user_sex

      const checkedCount = value.length;
      this.checkAllWeekDay = checkedCount === this.form.weekDay.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.weekOptions.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();



      const ruleId = row.ruleId || this.ids
      this.queryy.vall=row.course

      simpleListCla(this.queryy).then(response => {
        this.claOptions = response.data;

      }),
      getRule(ruleId).then(response => {


        this.form = response.data;

        this.form.weekDay = this.form.weekDay.split(",");
        this.open = true;
        this.title = "修改上课时间配置规则";
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.weekDay = this.form.weekDay.join(",");
          if (this.form.ruleId != null) {
            updateRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ruleIds = row.ruleId || this.ids;
      this.$modal.confirm('是否确认删除上课时间配置规则编号为"' + ruleIds + '"的数据项？').then(function() {
        return delRule(ruleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/rule/export', {
        ...this.queryParams
      }, `rule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" >
// 新增form 大小
.add-form {
  .el-select .el-input {
    width: 50px;
  }
  .el-input, .el-input-number{
    width: 50px;
  }
  .auto-width .el-input{
    width: 50px;
  }
  .el-date-editor.el-input{
    width: 50px;
  }
  &.auto-width {
    .el-date-editor.el-input, .el-date-editor.el-input__inner, .el-input, .el-input-number {
      width: auto;
    }
  }
}

.auto-width {
  .el-date-editor.el-input, .el-date-editor.el-input__inner, .el-input, .el-input-number {
    width: auto;
  }}
</style>
